/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views.GestionRestaurateur;

import Model.Expertise;
import Model.Gender;
import Model.Restaurateur;
import Views.GestionRestaurateur.ExpertiseEditor;
import Views.GestionRestaurateur.ExpertiseRenderer;
import Views.GestionRestaurateur.GenderEditor;
import Views.GestionRestaurateur.GenderRenderer;
import Views.GestionRestaurateur.RestaurateurTableListener;
import Views.GestionRestaurateur.RestaurateurTableModel;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author ibrahim
 */
public class RestaurateurTablePanel extends JPanel {
    
    private JTable table;
    private RestaurateurTableModel tableModel;
    private JPopupMenu popupMenu;
    private RestaurateurTableListener rtl;
    
    public RestaurateurTablePanel(){
        this.tableModel = new RestaurateurTableModel();
        table = new JTable(tableModel);
        popupMenu = new JPopupMenu();
        this.table.setDefaultRenderer(Expertise.class, new ExpertiseRenderer());
        this.table.setDefaultRenderer(Gender.class, new GenderRenderer());
        this.table.setDefaultEditor(Expertise.class, new ExpertiseEditor());
        this.table.setDefaultEditor(Gender.class, new GenderEditor());
        this.table.setRowHeight(25);
        setLayout(new BorderLayout());
        add(new JScrollPane(table),BorderLayout.CENTER);
        JMenuItem removeItem = new JMenuItem("Delete row");
        popupMenu.add(removeItem);
        removeItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
             int row = table.getSelectedRow();
             
             if(rtl!=null){
                 rtl.rowDeleted(row);
                 tableModel.fireTableRowsDeleted(row, row);
             }
             
             
            }
        });
        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                table.getSelectionModel().setSelectionInterval(row, row);
       
               if(e.getButton()== MouseEvent.BUTTON3){
                 popupMenu.show(table,e.getX(),e.getY());
               }
            }
        
        
        
        
        });
    
    
    }
    public void setRestaurateurTableListener(RestaurateurTableListener l){
        this.rtl=l;
    }
    public void setData(List<Restaurateur>db){
        tableModel.setData(db);
    }
    public void refresh(){
        tableModel.fireTableDataChanged();
    }
}
